<?php

namespace page\models;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "menu".
 *
 * @property integer $id Menu id(autoincrement)
 * @property string $name Menu name
 * @property integer $parent Menu parent
 * @property string $route Route for this menu
 * @property integer $order Menu order
 * @property string $icon Extra information for this menu
 *
 * @property Menu $menuParent Menu parent
 * @property Menu[] $menus Menu children
 *
 * @author Misbahul D Munir <misbahuldmunir@gmail.com>
 * @since 1.0
 */
class Pubport extends \yii\db\ActiveRecord
{
    public static  $statusOptions = array(
        '0' => '已下线',
        '1' => '上线中',
        '2' => '已上线',
    );
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%pubport}}';
    }

    /**
     * @inheritdoc
     */
    public static function getDb()
    {
        return Yii::$app->getDb();
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'node_id', 'template_id', 'uri',], 'required'],
            [['urlrule_id'], 'integer'],
            [['uri'], 'string'],
            [['status'], 'in',
                'range' => [-2, -1, 0, 1, 2],
                'message' => '调用方式 "{value}" 不合法.']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '名称',
            'uri' => 'URI',
            'node_id' => '站点ID',
            'template_id' => '模板ID',
            'urlrule_id' => 'URL规则ID',
            'status' => '状态',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
            'admin_id' => '账户ID',
        ];
    }

    public function beforeValidate(){
        if($this->getIsNewRecord()) {
            $this->create_time = time();
        }
        $this->update_time = time();
        $this->admin_id = Yii::$app->user->id;
        return true;
    }

    public function getStatusText($status) {
        return empty(self::$statusOptions[$status])?'未知':self::$statusOptions[$status];
    }

}